package com.dries.portal.module.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dries.common.api.CommonResult;
import com.dries.portal.module.portal.entity.Category;
import com.dries.portal.module.portal.service.ICategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 商品三级分类
 *
 * @author dries
 * @email samphsanie@gmail.com
 * @date 2020-08-06 10:50:19
 */
@Slf4j
@Api(tags = "商品分类管理API", value = "CategoryController")
@RestController
@RequestMapping("/portal/category")
public class CategoryController {

    @Resource
    private ICategoryService categoryService;

    /**
     * 列表
     */
    @ApiOperation("商品分类列表")
    @GetMapping("/list")
    public CommonResult list(@RequestParam(defaultValue = "10", required = false) Integer pageSize,
                             @RequestParam(defaultValue = "1", required = false) Integer pageNumber){

        log.info("分页数据==========》 分页大小：{}, 页码：{}", pageSize, pageNumber );
        PageHelper.startPage(pageNumber,pageSize);
        List<Category> list = categoryService.list(
                new QueryWrapper<Category>().lambda().eq(Category::getShowStatus, 1)
                        .orderByAsc(Category::getSort)
        );
        PageInfo<Category> pageInfo = new PageInfo<>(list);

        return CommonResult.success(pageInfo);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{catId}")
    public CommonResult info(@PathVariable("catId") Long catId){
		Category category = categoryService.getById(catId);

        return CommonResult.success(category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public CommonResult save(@RequestBody Category category){
		categoryService.save(category);

        return CommonResult.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public CommonResult update(@RequestBody Category category){
		categoryService.updateById(category);

        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public CommonResult delete(@RequestBody Long[] catIds){
		categoryService.removeByIds(Arrays.asList(catIds));

        return CommonResult.success();
    }

}
